<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<meta content="yes" name="apple-mobile-web-app-capable">
	<meta content="yes" name="apple-touch-fullscreen">
	<meta content="telephone=no,email=no" name="format-detection">
	<meta name="viewport" content="initial-scale=1, maximum-scale=1, minimum-scale=1, user-scalable=no, user-scalable=0">

	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
	<meta http-equiv="X-UA-Compatible" content="IE=8">
	<meta http-equiv="Expires" content="0">
	<meta http-equiv="Pragma" content="no-cache">
	<meta http-equiv="Cache-control" content="no-cache">
	<meta http-equiv="Cache" content="no-cache">

	<link rel="shortcut icon" href="http://hxypic.oss-cn-hangzhou.aliyuncs.com/bitbug_favicon.ico">
	<link rel="stylesheet" href="static/css/reset.css">
	<link rel="stylesheet" href="static/css/couponDetail.css">
	<script src="static/js/qrcode.js"></script>
	<script src="static/js/jweixin-1.2.0.js"></script>
	<script src="./../../static/apiUrl/index.js"></script>
	<!-- <link rel="stylesheet" href="//at.alicdn.com/t/font_360615_2job6ol0gujtt9.css"> -->
	<title>优惠券详情</title>
	<script>
		function resizes (fonts,initW,minW) {
		    var myIndoc=window.document.documentElement;
		    var myIndocWidth=window.document.documentElement.clientWidth ;
		    if(minW&&myIndocWidth<=minW){myIndocWidth=min};
		    var myInft=fonts*myIndocWidth/initW;
		    myIndoc.style.fontSize=myInft+'px';
		    this.rem=myInft;
		    this.precent=myIndocWidth/initW;
		    return  {rem:myInft, percent:myIndocWidth/initW};
		};

		resizes(100,750,window);
	</script>
</head>
<body>
	<div class="couponDetail" id="couponDetail">
	 	<div class="shareBg" id="shareBg"></div>
		<div class="top flex flex-align-center flex-pack-center">
			<div class="top-inner">
				<!-- <p class="title">商品抵用券</p>
				<p class="product-name">888</p> -->
			</div>
		</div>
		<div class="content-box">
			<div class="item-box">
	           <!--  <div class="item">
	                <span class="name">使用时间：</span>
	                <span class="value">2017-4-12~2018-01-01</span>
	            </div>
	            <div class="item">
	                <span class="name">优惠券类型：</span>
	                <span class="value">商品抵用券</span>
	            </div>
	            <div class="item">
	                <span class="name">使用时间：</span>
	                <span class="value">2017-4-12~2018-01-01</span>
	            </div>
	            <div class="item">
	                <span class="name">优惠券类型：</span>
	                <span class="value">商品抵用券</span>
	            </div> -->
	        </div>
	        <div class="rules-box">
	        	<!-- <div class="item">
	        		<div class="name">优惠说名：</div>
	        		<p class="text">优惠券类型：优惠券类型：优惠券类型：优惠券类型：优惠券类型：优惠券类型：优惠券类型：优惠券类型：优惠券类型：优惠券类型：优惠券类型：优惠券类型：优惠券类型：</p>
	        	</div>
	        	<div class="item">
	        		<div class="name">使用规则：</div>
	        		<p class="text">优惠券类型：优惠券类型：优惠券类型：优惠券类型：优惠券类型：优惠券类型：优惠券类型：优惠券类型：优惠券类型：优惠券类型：优惠券类型：优惠券类型：优惠券类型：</p>
	        	</div> -->
	        </div>
		</div>
		<div class="qrCode-box" style="padding-top:10px">
			<!-- <div class="qrCode" id="qrCode"></div> -->
			<!-- <p class="code">56515588485451</p> -->
		</div>
	</div>

		<script>
			window.onload = function() {
				if ( !window.location.search ) {
					alert('优惠券获取失败！请重新登录！')
					return;
				}

				var searchStr = window.location.search.split('?')[1];
				var searchArr = searchStr.split('&');
				var searchObj = {};
				for (var i = 0; i < searchArr.length; i++) {
					var curParam = searchArr[i].split('=');
					searchObj[curParam[0]] = curParam[1];
				}
				let couponType={
					'GET_ITEM_FREE':'商品抵用券'
				}
				ajax( 'GET', API_URL+'v1/admin/'+searchObj.id+'/coupon-instance', true, {}, function ( data ) {
					// 顶部部分
					let topInner = document.getElementsByClassName('top-inner')[0];
					let topInnerHtml = '<p class="title">'+data.title+'</p><p class="product-name">-'+data.productName+'-</p>';
					topInner.innerHTML = topInnerHtml;
					// 中间部分
					let contentBox = document.getElementsByClassName('content-box')[0];
					let contentHtml='<div class="item-box">'+
							            '<div class="item">'+
							                '<span class="name">店铺名称：</span>'+
							                '<span class="value">'+data.storeName+'</span>'+
							            '</div>'+
							            '<div class="item">'+
							                '<span class="name">店铺地址：</span>'+
							                '<span class="value">'+data.storeAddress+'</span>'+
							            '</div>'+
							            '<div class="item">'+
							                '<span class="name">到期时间：</span>'+
							                '<span class="value">'+new Date(data.endTime).Format('yyyy/MM/dd hh:mm')+'</span>'+
							            '</div>'+
							            '<div class="item">'+
							                '<span class="name">优惠券类型：</span>'+
							                '<span class="value">'+couponType[data.type]+'</span>'+
							            '</div>'+
							        '</div>'+
							        '<div class="rules-box">'+
							        	'<div class="item">'+
							        		'<div class="name">优惠说明：</div>'+
							        		'<p class="text">'+data.description+'</p>'+
							        	'</div>'+
							        	'<div class="item">'+
							        		'<div class="name">使用规则：</div>'+
							        		'<p class="text">'+data.notice+'</p>'+
							        	'</div>'+
							        '</div>';
					contentBox.innerHTML = contentHtml;
					// 二维码部分 （换一换写法,哈哈)
					let qrCodeBox = document.getElementsByClassName('qrCode-box')[0];
					let createdQrCode = document.createElement("div");
					let createdP = document.createElement("p");
					createdP.setAttribute("class", "code");
					createdP.innerHTML= data.code;
					createdQrCode.setAttribute("class", "qrCode");
					createdQrCode.setAttribute("id", "qrCode");
					// qrCodeBox.appendChild(createdQrCode);
					qrCodeBox.appendChild(createdP);
					let qrcode = new QRCode("qrCode", {
	                    text: data.code,
	                    colorDark : "#000000",
	                    colorLight : "#ffffff",
	                    correctLevel : QRCode.CorrectLevel.H
	                });
	                //分享
					//let shareUrl =  window.location.href.split("#")[0];
					let shareUrl =  encodeURIComponent(window.location.href.split("#")[0]);
			        ajax('GET', API_URL+'wechat/signature?url='+shareUrl, true, {}, function ( res ) {
			            wx.config({
			                debug: false,
			                appId: res.appId,
			                timestamp: res.timestamp,
			                nonceStr: res.nonceStr,
			                signature: res.signature,
			                jsApiList: [
			                    'checkJsApi',
			                    'onMenuShareTimeline',
			                    'onMenuShareAppMessage',
			                    'onMenuShareQQ',
			                    'onMenuShareWeibo',
			                    'onMenuShareQZone',
			                    'hideMenuItems'
			                ]
			            });
			            wx.ready(function(){
			                if(!data.shareable){
			                	 wx.hideMenuItems({
			                        menuList: [
			                            'menuItem:share:timeline', // 分享到朋友圈
			                            'menuItem:share:appMessage', // 发送给朋友
			                            'menuItem:share:QZone', // 分享到 QQ 空间
			                            'menuItem:share:qq', // 分享到QQ
			                        ],
			                        success: function (res) {
			                            
			                        },
			                        fail: function (res) {
			                            
			                        }
			                    });
			                	return
			                };
			                let shareBg = document.getElementById('shareBg');
			                shareBg.className ="shareBg1";
			                shareBg.onclick = function(){
			                	shareBg.className ="shareBg";
			                }
						    let shareTitle = data.title;
				            let shareLink = API_URL+'wechat/member/share-coupon?id='+searchObj.id;
				            let imgUrl = data.logoURL;
				            let notice = '使用规则：'+data.notice;
				            // console.log('可以分享')
				            //分享到朋友圈
				            wx.onMenuShareTimeline({
				                title: shareTitle,
				                link: shareLink,
				                imgUrl: imgUrl,
				                trigger: function (res) {

				                },
				                success: function (res) {
									addCount(searchObj.id,"SHARE");
				                    alert('分享朋友圈成功');
				                },
				                cancel: function (res) {
				                    
				                },
				                fail: function (res) {
				                    
				                }
				            });
				            //分享给朋友
				            wx.onMenuShareAppMessage({
				                title: shareTitle, // 分享标题
				                desc: notice, // 分享描述
				                link: shareLink, // 分享链接，该链接域名或路径必须与当前页面对应的公众号JS安全域名一致
				                imgUrl: imgUrl, // 分享图标
				                type: 'link', // 分享类型,music、video或link，不填默认为link
				                dataUrl: '', // 如果type是music或video，则要提供数据链接，默认为空
				                success: function () {
								   addCount(searchObj.id,"SHARE");
				                   alert('分享朋友成功')
				                },
				                cancel: function () { 
				                    
				                },
				                fail: function (res) {
				                   
				                }
				            });
						});
			        })
				});

			}


			/**
			 * 封装请求响应的函数
			 * type : 请求类型( GET/POST ) 
			 * url  : 请求地址
			 * async: 同步异步( flase/true )
			 * data : 传入的数据
			 * fn   : 回调函数
			 */
			function ajax(type, url, async, data, fn) {
			    // 创建xhr对象(兼容IE5,6)
			    var request;
			    if (window.XMLHttpRequest) {
			        request = new XMLHttpRequest();
			    } else {
			        request = new window.ActiveXObject('Microsoft.XMLHTTP');
			    }

			    // 打开
			    request.open(type, url, async);

			    // 如果是POST，为POST设置请求头
			    if (type.toUpperCase() === 'POST') {
			        request.setRequestHeader('Content-Type', 'application/JSON');
			    }

			    // 发送数据
			    // POST方式的时候，就传data参数
			    // GET方式的时候，传入data无效
			    request.send(JSON.stringify(data));

			    // 接收数据
			    request.onreadystatechange = function() {
			        if ( request.readyState == 4 && request.status == 200) {
			            var oData;
			            if (request.responseText) {
			                oData = JSON.parse(request.responseText)
			            }
			            if ( fn ) {
			            	fn( oData );
			            }
			        } else if ( request.readyState == 4 ) {
			        	var text;
			        	var error;
			        	try {
				        	error = JSON.parse(request.responseText);
			        	} catch ( e ) {
			        		error = request.responseText;
			        	}
			        	error.message?text=error.message:text=error;
			        	alert(text);
			        }
			    }
			}

			function addCount(id,type){
				ajax('GET', API_URL+'wechat/add-count?id='+id+"&type="+type, true, {}, function ( res ) {
					console.log("add");
				});
			}

			Date.prototype.Format = function (fmt) { //author: meizz 
			    var o = {
			        "M+": this.getMonth() + 1, //月份 
			        "d+": this.getDate(), //日 
			        "h+": this.getHours(), //小时 
			        "m+": this.getMinutes(), //分 
			        "s+": this.getSeconds(), //秒 
			        "q+": Math.floor((this.getMonth() + 3) / 3), //季度 
			        "S": this.getMilliseconds() //毫秒 
			    };
			    if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
			    for (var k in o)
			    if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
			    return fmt;
			}

		</script>
</body>
</html>